﻿using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Warehouse
{
    public class PromotionSalesFullCutApprovementDto : EntityDto
    {
        public int operatorId { get; set; }
        public string receiptNo { get; set; }
    }

    public interface IPromotionSalesFullCutService : IApplicationService
    {
        Task ApprovePromotionSalesFullCut(PromotionSalesFullCutApprovementDto dto);
        Task RejectPromotionSalesFullCut(PromotionSalesFullCutApprovementDto dto);
        Task<PromoteSalesFullCut[]> AttachWarehouse(PromoteSalesFullCut[] data);
        Task<PromoteSalesFullCut[]> AttachGoodsCategory(PromoteSalesFullCut[] data);
    }

    public class PromotionSalesFullCutService : MyApplicationService, IPromotionSalesFullCutService
    {
        private readonly IRepository<PromoteSalesFullCut> promoteSalesFullCuts;
        public PromotionSalesFullCutService(IRepository<PromoteSalesFullCut> promoteSalesFullCuts)
        {
            this.promoteSalesFullCuts = promoteSalesFullCuts;
        }
        //add by cl 20240311
        //满减分类关联
        public async Task<PromoteSalesFullCut[]> AttachGoodsCategory(PromoteSalesFullCut[] data)
        {
            var db = await this.promoteSalesFullCuts.GetDbContextAsync();

            var ids = data.Select(x => x.Id).ToArray();

            var discountMap = await db.Set<PromoteSalesFullCutCategory>().AsNoTracking().Where(x => ids.Contains(x.fullCutID)).ToArrayAsync();
            await db.AttachData<GoodsCategory, PromoteSalesFullCutCategory>(discountMap, x => x.categoryID, (m, arr) => m.GoodsCategory = arr.FirstOrDefault());

            foreach (var m in data)
            {
                m.PromoteSalesFullCutCategory = discountMap.Where(x => x.fullCutID == m.Id).ToArray();
            }

            return data;
        }


        public async Task<PromoteSalesFullCut[]> AttachWarehouse(PromoteSalesFullCut[] data)
        {
            var db = await this.promoteSalesFullCuts.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, PromoteSalesFullCut>(data,
                idGetter: x => x.warehouseID,
                action: (m, arr) => m.Warehouse = arr.FirstOrDefault());

            return data;
        }

        void CheckPromotionSalesFullCutApprovementDto(PromotionSalesFullCutApprovementDto dto)
        {
            dto.Should().NotBeNull();
            dto.receiptNo.Should().NotBeNullOrEmpty();
        }

        public async Task ApprovePromotionSalesFullCut(PromotionSalesFullCutApprovementDto dto)
        {
            this.CheckPromotionSalesFullCutApprovementDto(dto);

            var db = await this.promoteSalesFullCuts.GetDbContextAsync();

            var items = await db.Set<PromoteSalesFullCut>().AsTracking().Where(x => x.receiptNo == dto.receiptNo).ToArrayAsync();
            if (!items.Any())
            {
                throw new UserFriendlyException("数据不存在");
            }

            foreach (var m in items)
            {
                if (m.isAuditing)
                {
                    throw new UserFriendlyException("已经审批");
                }

                m.isAuditing = true;
                m.operateUserID = dto.operatorId;
                m.operateTime = this.Clock.Now;
            }
            await db.SaveChangesAsync();
        }

        public async Task RejectPromotionSalesFullCut(PromotionSalesFullCutApprovementDto dto)
        {
            this.CheckPromotionSalesFullCutApprovementDto(dto);

            var db = await this.promoteSalesFullCuts.GetDbContextAsync();

            var items = await db.Set<PromoteSalesFullCut>().AsTracking().Where(x => x.receiptNo == dto.receiptNo).ToArrayAsync();
            if (!items.Any())
            {
                throw new UserFriendlyException("数据不存在");
            }

            foreach (var m in items)
            {
                if (!m.isAuditing)
                {
                    throw new UserFriendlyException("未审批");
                }

                m.isAuditing = false;
                m.operateUserID = dto.operatorId;
                m.operateTime = this.Clock.Now;
            }
            await db.SaveChangesAsync();
        }
    }
}
